import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { resetGlobalState } from '@/utils/globalState'
import { loginRoutes } from './modules/login'
import { adminRoutes } from './modules/admin'
import { userRoutes } from './modules/user'

const routes: RouteRecordRaw[] = [
  ...loginRoutes,
  ...adminRoutes,
  ...userRoutes
]

const router = createRouter({
  history: createWebHistory(),
  routes
})



router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    resetGlobalState()
    next()
  }
})




export default router